import React from 'react'
import { Route, Routes, useLocation, Navigate } from "react-router-dom"
import Login from "../pages/login"
import Reguser from "../pages/reguser"
import { getCookie } from '../untils/auth'
import MainNav from "../pages/mainNav"
import Home from '../pages/home/home'
import MedicalTreatment from '../pages/medicalTreatment/medicalTreatment'
import Message from '../pages/message/message'
import My from "../pages/my/my"
import HospitalIntroduction from "../pages/hospitalIntroduction/hospitalIntroduction"
import HospitalAnnouncement from "../pages/hospitalAnnouncement/hospitalAnnouncement"
import HospitalAnnouncementDesc from "../pages/hospitalAnnouncementDesc/hospitalAnnouncementDesc"
import AnnouncementDesc from "../pages/announcementDesc/announcementDesc"
import DoctorCard from "../pages/doctorCard/doctorCard"
import AddDoctorCard from '../pages/addDoctorCard/addDoctorCard'
import SelectMedicalCard from '../pages/selectMedicalCard/selectMedicalCard'
import SelectDoctor from "../pages/selectDoctor/selectDoctor"
import AppointmentRegister from "../pages/appointmentRegister/appointmentRegister"
import ReservationRecord from "../pages/reservationRecord/reservationRecord"
import Department from "../pages/department/department"
import Map from "../pages/map/map"
export default function index() {
    const location = useLocation()
    const { pathname } = location
    const router = [
        {
            path: "/login",
            element: <Login />,
            auth: false
        },
        {
            path: "/reguser",
            element: <Reguser />,
            auth: false
        },
        {
            path: "/home",
            element: <MainNav />,
            auth: true,
            children: [
                {   //首页
                    path: "/home",
                    element: <Home></Home>,
                    auth: true
                },
                {   //在线问诊
                    path: "/home/medicalTreatment",
                    element: <MedicalTreatment></MedicalTreatment>,
                    auth: true
                },
                {  //消息
                    path: "/home/message",
                    element: <Message></Message>,
                    auth: true
                },
                {   //我的
                    path: "/home/my",
                    element: <My></My>,
                    auth: true
                },
                {  //医院介绍
                    path: "/home/hospitalIntroduction",
                    element: <HospitalIntroduction></HospitalIntroduction>,
                    auth: true
                },
                {   //医院公告列表
                    path: "/home/hospitalAnnouncement",
                    element: <HospitalAnnouncement></HospitalAnnouncement>,
                    auth: true
                },
                {   //医院公告详情
                    path: "/home/hospitalAnnouncementDesc/:id",
                    element: <HospitalAnnouncementDesc></HospitalAnnouncementDesc>,
                    auth: true
                },
                {   //医院公告详情
                    path: "/home/announcementDesc/:id",
                    element: <AnnouncementDesc></AnnouncementDesc>,
                    auth: true
                },
                {   //就诊卡
                    path: "/home/doctorCard",
                    element: <DoctorCard></DoctorCard>,
                    auth: true
                },
                {   //添加就诊卡
                    path: "/home/addDoctorCard/:id",
                    element: <AddDoctorCard></AddDoctorCard>,
                    auth: true
                },
                {   //选择就诊卡
                    path: "/home/selectMedicalCard",
                    element: <SelectMedicalCard></SelectMedicalCard>,
                    auth: true
                },
                {   //选择就诊卡
                    path: "/home/selectDoctor/:cardId",
                    element: <SelectDoctor></SelectDoctor>,
                    auth: true
                },
                {   //预约挂号
                    path: "/home/appointmentRegister/:cardId/:doctorId",
                    element: <AppointmentRegister></AppointmentRegister>,
                    auth: true
                },
                {   //预约记录
                    path: "/home/reservationRecord",
                    element: <ReservationRecord></ReservationRecord>,
                    auth: true
                },
                {   //预约记录
                    path: "/home/department",
                    element: <Department></Department>,
                    auth: true
                },
                {   //预约记录
                    path: "/home/map",
                    element: <Map></Map>,
                    auth: true
                },
            ]
        },
        {
            path: "/",
            element: <Navigate to="/home" />,
        },
    ]
    const RouteNav = (router) => {
        return (
            router.map((item) => {
                return (
                    <Route path={item.path} element={item.auth && !getCookie("token") ?
                        <Navigate to='/login' replace={true}></Navigate> :
                        getCookie("token") && pathname === "/login" ?
                            <Navigate to="/home"></Navigate> : item.element} key={item.path}>
                        {
                            item.children && RouteNav(item.children)
                        }
                    </Route>
                )
            })
        )
    }
    return (
        <Routes>
            {RouteNav(router)}
        </Routes>
    )
}
